package com.tianxia.lib.baseworld.alipay;

import org.json.JSONObject;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.utils.ItktLog;

/**
 * 对签名进行验签
 * 
 */
public class ResultChecker {

	public static final int RESULT_INVALID_PARAM = 0;
	public static final int RESULT_CHECK_SIGN_FAILED = 1;
	public static final int RESULT_CHECK_SIGN_SUCCEED = 2;

	String mContent;

	public ResultChecker(String content) {
		this.mContent = content;
	}

	// /**
	// * 从验签内容中获取成功状态
	// *
	// * @return
	// */
	// private String getSuccess() {
	// String success = null;
	//
	// try {
	// JSONObject objContent = BaseHelper.string2JSON(this.mContent, ";");
	// String result = objContent.getString("result");
	// result = result.substring(1, result.length() - 1);
	//
	// JSONObject objResult = BaseHelper.string2JSON(result, "&");
	// success = objResult.getString("success");
	// success = success.replace("\"", "");
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	//
	// return success;
	// }

	/**
	 * 对签名进行验签
	 * 
	 * @return
	 */
	public int checkSign() {
		int retVal = RESULT_CHECK_SIGN_SUCCEED;

		try {
			JSONObject objContent = BaseHelper.string2JSON(this.mContent, ";");
			String result = objContent.getString("result");
			result = result.substring(1, result.length() - 1);
			
			
			ItktLog.e("**-------@@-------result----------@@--------**"+result);
			// 获取待签名数据
			int iSignContentEnd = result.indexOf("&sign=");
			int iSignContentEnd2=result.indexOf("&subject=");
			String signContent = result.substring(0, iSignContentEnd);
			String signContent2 = result.substring(iSignContentEnd2, result.length());
			String signContent3=signContent+signContent2;
			ItktLog.e("**-------@@-------signContent3----------@@--------**"+signContent3);
			
			// 获取签名
			JSONObject objResult = BaseHelper.string2JSON(result, "&");
			String signType = objResult.getString("sign_type");
			signType = signType.replace("\"", "");

			String sign = objResult.getString("sign");
//			sign = sign.replace("\"", "");
			
			ItktLog.e("**-------@@-------sign----------@@--------**"+sign);
			// 进行验签 返回验签结果
			if (signType.equalsIgnoreCase("RSA")) {
				if (!Rsa.doCheck(signContent3, sign, PartnerConfig.RSA_ALIPAY_PUBLIC))
					retVal = RESULT_CHECK_SIGN_FAILED;
			}
		} catch (Exception e) {
			retVal = RESULT_INVALID_PARAM;
			e.printStackTrace();
		}

		return retVal;
	}

	// boolean isPayOk() {
	// boolean isPayOk = false;
	//
	// String success = getSuccess();
	// if (success.equalsIgnoreCase("true")
	// && checkSign() == RESULT_CHECK_SIGN_SUCCEED)
	// isPayOk = true;
	//
	// return isPayOk;
	// }
}